package com.tanhua.app.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.pojo.BlackList;
import com.tanhua.model.pojo.Question;
import com.tanhua.model.pojo.Settings;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    //查询用户通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        vo.setId(UserHolder.getUserId());
        vo.setPhone(UserHolder.getMobile());
        //1.查询陌生人问题
        Question question = questionApi.findByUserId(vo.getId());
        //设置一个默认问题,如果查询为空则设置一个默认问题
        String txt = question == null ? "你喜欢java吗?" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //2.查询默认设置
        Settings settings = settingsApi.findByUserId(vo.getId());
        if (settings != null) {
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        //3.返回结果
        return vo;
    }

    //设置陌生人问题
    public void questions(String content) {
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.查询当前用户是否有陌生人问题
        Question question = questionApi.findByUserId(userId);
        //3.如果没有有则添加陌生人问题
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            //4.如果有则修改陌生人问题
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    //保存设置信息
    public void saveSetting(Map map) {
        //1.获取数据
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        //2.获取userId 然后去查询设置信息
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);
        //3.如果查询到的结果为空则保存设置信息
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
        //4.如果不为空则修改设置信息
    }

    //黑名单分页展示
    public PageResult<UserInfo> blacklist(Integer page, Integer pagesize) {
        //1.获取当前用户的id
        Long userId = UserHolder.getUserId();
        //2.调用api获取Page对象
        Page<UserInfo> infoPage = userInfoApi.findBlackList(userId,page,pagesize);
        //3.将page对象封装到PageResult中
        PageResult<UserInfo> pageResult = new PageResult<UserInfo>(page,pagesize, (int) infoPage.getTotal(),infoPage.getRecords());
        //4.返回结果
        return pageResult;
    }

    //移除黑名单
    public void removeBlacklist(Long id) {
        Long userId = UserHolder.getUserId();
        blackListApi.removeBlackList(userId,id);
    }
}
